package modeladesktop.gui;
import javax.swing.*;
import java.util.*;
import java.io.*;
import java.awt.event.*;

public class PanelMain extends JPanel implements ActionListener{
	private JLabel nombreL = new JLabel("Nombre del proyecto");
	private JLabel variablesL = new JLabel("Variables");

	private ModeloVariables modelo = new ModeloVariables();
	private JTextField nombreTF = new JTextField();
	private JTable variablesT = new JTable(modelo);
	private JButton addB = new JButton("Agregar");
	private JButton rmB = new JButton("Eliminar");
	private JButton createB = new JButton("Crear");
	private JButton loadB = new JButton("Cargar Archivo");

	public PanelMain(){
		setLayout(null);
		JScrollPane scroll = new JScrollPane(variablesT);
		addB.addActionListener(this);
		rmB.addActionListener(this);
		createB.addActionListener(this);
		loadB.addActionListener(this);

		nombreL.setBounds(20,20, 150, 25);
		nombreTF.setBounds(170, 20, 200, 30);
		variablesL.setBounds(20, 45, 100, 25);
		scroll.setBounds(20, 65, 200, 200);
		addB.setBounds(230, 65, 100, 25);
		rmB.setBounds(230, 95, 100, 25);
		createB.setBounds(230, 125, 100, 25);
		loadB.setBounds(230, 155, 100, 25);

		add(nombreL);
		add(nombreTF);
		add(variablesL);
		add(scroll);
		add(addB);
		add(rmB);
		add(createB);
		add(loadB);
	}

	public void actionPerformed(ActionEvent ev){
		if(ev.getSource() == addB){
			modelo.addRow();
		}else if(ev.getSource() == rmB){
			if(variablesT.getSelectedRow() != -1)
				modelo.removeRow(variablesT.getSelectedRow());
		} else if(ev.getSource() == loadB) {
			JFileChooser choos = new JFileChooser();
			int op = choos.showOpenDialog(this);
			ArrayList arr = new ArrayList();
			ArrayList <ArrayList <Double>> mapa = new ArrayList <ArrayList <Double>> ();
			if(op == JFileChooser.APPROVE_OPTION){
				File arch = choos.getSelectedFile();
				BufferedReader read = null;
				try{
					read = new BufferedReader(new FileReader(arch));
					boolean cabecera = true;
					while(true){
						String linea = read.readLine();
						if(linea == null)
							break;
						StringTokenizer tock = new StringTokenizer(linea, ",");
						if(cabecera){
							while(tock.hasMoreTokens())
								arr.add(tock.nextToken());
							cabecera = false;
						} else {
							ArrayList <Double> fila = new ArrayList <Double> ();
							while(tock.hasMoreTokens())
								fila.add(Double.parseDouble(tock.nextToken()));
							mapa.add(fila);
						}
					}
				}catch(IOException ex){}
				JTabbedPane principal = (JTabbedPane)getParent();
				principal.add("Variables", new PanelTabla(arr.toArray(), mapa));
			}
		} else if(ev.getSource() == createB){
			if(variablesT.getRowCount() < 2)
				JOptionPane.showMessageDialog(this, "Debe ingresar al menos dos variables", "Error", JOptionPane.ERROR_MESSAGE);
			else if(modelo.hasNull())
				JOptionPane.showMessageDialog(this, "Hay una variable sin nombre", "Error", JOptionPane.ERROR_MESSAGE);
			else if(modelo.hasRepetida())
				JOptionPane.showMessageDialog(this, "Hay variables con el mismo nombre", "Error", JOptionPane.ERROR_MESSAGE);
			else {
				JTabbedPane principal = (JTabbedPane)getParent();
				principal.add("Variables", new PanelTabla(modelo.getVars().toArray() ));
			}
		}
	}
}
